package com.jlkf.controller;

import com.jlkf.common.Constant;
import com.platform.server.response.pojo.ReturnData;
import com.platform.server.util.ImageCompressUtil;
import com.platform.server.util.StringTools;
import com.platform.server.util.UUIDUtil;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import javax.servlet.http.HttpServletRequest;


import java.io.File;
import java.net.InetAddress;
import java.util.*;

/**图片上传
 * Created by Administrator on 2017/2/23 0023.
 */
@Controller
@RequestMapping("/pcphoto")
public class PCPhotoUploadController extends BaseController {

    @ResponseBody
    @RequestMapping(value = "/pcphotoImg",method = RequestMethod.POST)
    public Object pCPhotoUpload(String type, String needsThumb, HttpServletRequest request)throws Exception{
        try {
            String  dir= "fab";         
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            StringBuffer pictureUrls =new StringBuffer();
            List<String> imagePath = new ArrayList<>();
            if (multipartResolver.isMultipart(request)) {
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                Iterator<String> iter = multiRequest.getFileNames();
                System.out.println(iter+"iter");
                InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
                String hostAddress = address.getHostAddress();
                if(iter.hasNext()){
                    while (iter.hasNext()) {
                        MultipartFile file = multiRequest.getFile((String) iter.next());
                        //文件名称
                        String fileName = file.getOriginalFilename();
                        if (!fileName.equals("")) {
								/*String path1 = Thread.currentThread()
										.getContextClassLoader().getResource("").getPath() + "dowload" + File.separator;*/
                            //获得文件后缀名称
                            String prefix =fileName.substring(fileName.lastIndexOf(".")+1);
                            //图片文件放的位置文件夹目录
                            String path = "";
                          //  if(prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("bmp")){
                                // 项目在容器中实际发布运行的根路径
                                String realPath =  request.getSession().getServletContext().getRealPath("/");
                                String path2 = realPath.substring(0,realPath.lastIndexOf(System.getProperty("file.separator")));
                                String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator"))))+System.getProperty("file.separator")+"file"+System.getProperty("file.separator")+dir;
                                // 自定义的文件名称
                                String trueFileName= UUIDUtil.getOrderSnByTime18()+"."+prefix;
                                // 设置存放图片文件的路径
                                path=p+System.getProperty("file.separator")+trueFileName;                                                             
                                //转为标准文件盘符格式
                                String acesP = "http://"+ Constant.SERVER_NAME+":"+request.getServerPort()+"//"+"file"+"//"+dir+"//"+trueFileName;
                                final File localFile = new File(path);
                                //将文件复制到指定文件夹下面
                                try {
                                    if(!localFile.exists()){
                                        localFile.mkdirs();
                                    }
                                    file.transferTo(localFile);
                                } catch (Exception e) {
                                }
                                imagePath.add(path);
                                StringBuffer sb = new StringBuffer();
                                sb.append(acesP);
                                sb.append(",");
                                pictureUrls.append(sb);
                           /* }else{
                                return new ReturnData(702,"系统暂不支持此格式图片");
                            }*/
                        }
                    }
                    String purl = "";
                    if(StringTools.isEmpty(pictureUrls.toString())){
                        return new ReturnData(703,"没有文件路径");
                    }else {
                        Map<String,String> retMap =new HashMap<>();
                        purl = pictureUrls.substring(0, pictureUrls.length() - 1);
                        retMap.put("purl", purl);
                            /*
                            是否要压缩图片
                            */
                       /* if(needsThumb!=null){
                        	if(StringTools.numIsGT0(needsThumb)&&Integer.parseInt(needsThumb)==1) {
                                StringBuffer thumbBuff = new StringBuffer();
                                for (String img : imagePath) {
                                    String thumb = ImageCompressUtil.saveMinPhoto(img, Constant.IMAGECOMPRESS_SMALLICON, Constant.IMAGECOMPRESS_COMBASE, Constant.IMAGECOMPRESS_SCALE);
                                    thumb = thumb.substring(thumb.lastIndexOf(System.getProperty("file.separator")));
                                    thumbBuff.append("http://" +Constant.SERVER_NAME+ ":" + request.getServerPort() + System.getProperty("file.separator") + "file" + System.getProperty("file.separator") + dir + System.getProperty("file.separator") + thumb + ",");
                                }
                                String thumbString = thumbBuff.substring(0, thumbBuff.length() - 1);
                                //设置缩略图即可
                                retMap.put("thumbUrl", thumbString);
                            }
                        }*/
                        return new ReturnData(purl,0,"success");
                    }
                }else {
                    return new ReturnData(701,"No Img");
                }
            }else{
                return new ReturnData(700,"No Img");
            }
        }catch (Exception e){
           return new ReturnData(400,"参数有误");
        }
    }
    @ResponseBody
    @RequestMapping(value = "/umpCPhotoUpload",method = RequestMethod.POST)
    public Object umpCPhotoUpload(String type, String needsThumb, HttpServletRequest request)throws Exception{
        try {
            String  dir= "fab";         
            CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver(request.getSession().getServletContext());
            StringBuffer pictureUrls =new StringBuffer();
            List<String> imagePath = new ArrayList<>();
            if (multipartResolver.isMultipart(request)) {
                MultipartHttpServletRequest multiRequest = (MultipartHttpServletRequest) request;
                Iterator<String> iter = multiRequest.getFileNames();
                System.out.println(iter+"iter");
                InetAddress address = InetAddress.getLocalHost();//获取的是本地的IP地址 //PC-20140317PXKX/192.168.0.121
                String hostAddress = address.getHostAddress();
                if(iter.hasNext()){
                    while (iter.hasNext()) {
                        MultipartFile file = multiRequest.getFile((String) iter.next());
                        //文件名称
                        String fileName = file.getOriginalFilename();
                        if (!fileName.equals("")) {
								/*String path1 = Thread.currentThread()
										.getContextClassLoader().getResource("").getPath() + "dowload" + File.separator;*/
                            //获得文件后缀名称
                            String prefix =fileName.substring(fileName.lastIndexOf(".")+1);
                            //图片文件放的位置文件夹目录
                            String path = "";
                            if(prefix.equalsIgnoreCase("png") || prefix.equalsIgnoreCase("jpg") || prefix.equalsIgnoreCase("bmp")){
                                // 项目在容器中实际发布运行的根路径
                                String realPath =  request.getSession().getServletContext().getRealPath("/");
                                String path2 = realPath.substring(0,realPath.lastIndexOf(System.getProperty("file.separator")));
                                String p = realPath.substring(0, (path2.lastIndexOf(System.getProperty("file.separator"))))+System.getProperty("file.separator")+"file"+System.getProperty("file.separator")+dir;
                                // 自定义的文件名称
                                String trueFileName= UUIDUtil.getOrderSnByTime18()+"."+prefix;
                                // 设置存放图片文件的路径
                                path=p+System.getProperty("file.separator")+trueFileName;
                                
                                String acesP = "http://"+Constant.SERVER_NAME+":"+ request.getServerPort()+"//"+"file"+"//"+dir+"//"+trueFileName;
                                //转为标准文件盘符格式
                               // String acesP = "http://"+Constant.SERVER_NAME+":"+ request.getServerPort()+"//"+"images"+"//"+dir+"//"+trueFileName;
                                final File localFile = new File(path);
                                //将文件复制到指定文件夹下面
                                try {
                                    if(!localFile.exists()){
                                        localFile.mkdirs();
                                    }
                                    file.transferTo(localFile);
                                } catch (Exception e) {
                                }
                                imagePath.add(path);
                                StringBuffer sb = new StringBuffer();
                                sb.append(acesP);
                                sb.append(",");
                                pictureUrls.append(sb);
                            }else{
                                return new ReturnData(702,"系统暂不支持此格式图片");
                            }
                        }
                    }
                    String purl = "";
                    if(StringTools.isEmpty(pictureUrls.toString())){
                        return new ReturnData(703,"没有图片路径");
                    }else {
                        Map<String,String> retMap =new HashMap<>();
                        purl = pictureUrls.substring(0, pictureUrls.length() - 1);
                        retMap.put("purl", purl);
                            /*
                            是否要压缩图片
                            */
                        if(StringTools.numIsGT0(needsThumb)&&Integer.parseInt(needsThumb)==1) {
                            StringBuffer thumbBuff = new StringBuffer();
                            for (String img : imagePath) {
                                String thumb = ImageCompressUtil.saveMinPhoto(img, Constant.IMAGECOMPRESS_SMALLICON, Constant.IMAGECOMPRESS_COMBASE, Constant.IMAGECOMPRESS_SCALE);
                                thumb = thumb.substring(thumb.lastIndexOf(System.getProperty("file.separator")));
                                thumbBuff.append("http://" +Constant.SERVER_NAME+ ":" + request.getServerPort() + System.getProperty("file.separator") + "file" + System.getProperty("file.separator") + dir + System.getProperty("file.separator") + thumb + ",");
                            }
                            String thumbString = thumbBuff.substring(0, thumbBuff.length() - 1);
                            //设置缩略图即可
                            retMap.put("thumbUrl", thumbString);
                        }
                        return new ReturnData(purl,0,"success");
                    }
                }else {
                    return new ReturnData(701,"未发现图片");
                }
            }else{
                return new ReturnData(700,"未发现图片");
            }
        }catch (Exception e){
           return new ReturnData(400,"参数有误");
        }
    }
   

}

